package keter.combofs.persist;

import com.alibaba.fastjson.JSONObject;
import com.google.common.io.Files;
import keter.combofs.path.PathMaker;
import keter.combofs.util.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * Created by gulx@neusoft.com on 2019/12/3.
 */
public class Fs {
    @Autowired
    private PathMaker pathMaker;

    private static String SUFFIX = ".meta";

    public static void save(JSONObject r) throws IOException {
        String metaFileUri = r.get("uri")+SUFFIX;
        Files.write(r.toJSONString().getBytes(),new java.io.File(metaFileUri));
    }

    public static void addMD5(String fileUri,String md5) throws IOException {
        JSONObject meta = find(fileUri);
        meta.put("md5",md5);
        save(meta);
    }

    public static JSONObject find(String fileUri) throws IOException {
        return JSONObject.parseObject(IOUtil.fileToString(fileUri+SUFFIX));
    }

    public static boolean delete(String fileUri) {
        return new java.io.File(fileUri+SUFFIX).delete();
    }
}
